﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.Xml;

using $prjsn$.Common;
using $prjsn$.Common.Data.Services;
using $prjsn$.Entity;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services.Xml
{
    public class ArticleGroupDao : BaseXmlDao<ArticleGroup, int, CodeListFilter>, IArticleGroupDao
    {
        #region Find
        /// <summary>
        ///     Fills the Criteria list with where statements based on provided filter
        /// </summary>
        /// <param name = "filter"></param>
        /// <returns></returns>
        public override IEnumerable<ArticleGroup> Find(CodeListFilter filter)
        {
            // Equal
            Criteria.Add(entity => entity.ID.IsEqual(filter.IdExact));
            Criteria.Add(entity => entity.Code.IsExact(filter.CodeExact)); // exact match even for string.Empty

            // Like
            Criteria.Add(entity => entity.Code.IsLike(filter.Code));
            Criteria.Add(entity => entity.Description.IsLike(filter.Description));

            Criteria.Add(entity => entity.IsVisible.IsEqual(filter.IsVisible));

            return GetListByCriteria(filter);
        }
        #endregion Find

        #region XML - ENTITY mapping
        public IEnumerable<ArticleGroup> GetAll()
        {
            return Find(new CodeListFilter { TakeRows = int.MaxValue });
        }

        /// <summary>
        ///     Converts the XElement into the ArticleGroup
        /// </summary>
        /// <param name = "element"></param>
        /// <returns></returns>
        protected override ArticleGroup CreateEntity(XElement element)
        {
            var entity = FillEntity(new ArticleGroup(), element); // base converts ValueTypes and String

            entity.SetId((int)element.Attribute(Str.Common.ID));
            return entity;
        }

        /// <summary>
        ///     Converts the ArticleGroup instnace into the XElement
        /// </summary>
        /// <param name = "entity"></param>
        /// <returns></returns>
        protected override XElement CreateElement(ArticleGroup entity)
        {
            var element = base.CreateElement(entity); // base converts ValueTypes and String

            return element;
        }
        #endregion XML - ENTITY mapping

        #region NextId
        /// <summary>
        ///     Incremented 'Next ID' generator.
        ///     Searches for the max existing used value
        ///     and returnes that + 1
        /// </summary>
        /// <returns></returns>
        protected override int GetNextId(ArticleGroup entity = null)
        {
            var id = 0; // in case there is no element yet
            if (TheList.Any()) // element(s) exist(s) - find the largest ID
            {
                id = TheList.Max<ArticleGroup, int>(e => e.ID);
            }
            return ++id;
        }
        #endregion NextId
    }
}